package net.lab1024.sa.admin.module.business.traffic.aibi.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Service;

import net.lab1024.sa.admin.module.business.traffic.aibi.domain.model.AibiCameraStatus;
import net.lab1024.sa.admin.module.business.traffic.aibi.domain.vo.AibiCameraStatusVO;

/**
 * 摄像机实时状态缓存服务
 */
@Service
public class AibiCameraStatusService {

    private final ConcurrentHashMap<String, AibiCameraStatus> statusMap = new ConcurrentHashMap<>();

    /**
     * 更新排队信息
     */
    public void updateQueues(String cameraCode, String roadCode, Integer deviceId,
                             Integer leftQueue, Integer straightQueue, Double ratio, LocalDateTime sampleTime) {
        statusMap.compute(cameraCode, (key, current) -> {
            if (current == null) {
                current = new AibiCameraStatus();
                current.setCameraCode(cameraCode);
            }
            current.setRoadCode(roadCode);
            current.setDeviceId(deviceId);
            current.setLeftQueueLength(leftQueue);
            current.setStraightQueueLength(straightQueue);
            current.setRatio(ratio);
            current.setLastSampleTime(sampleTime);
            return current;
        });
    }

    /**
     * 更新指令派发结果
     */
    public void updateLaneMode(String cameraCode, String mode, String requestId, String status,
                               String message, LocalDateTime switchTime) {
        statusMap.compute(cameraCode, (key, current) -> {
            if (current == null) {
                current = new AibiCameraStatus();
                current.setCameraCode(cameraCode);
            }
            current.setCurrentMode(mode);
            current.setLastCommandRequestId(requestId);
            current.setLastCommandStatus(status);
            current.setLastCommandMessage(message);
            current.setLastSwitchTime(switchTime);
            return current;
        });
    }

    /**
     * 查询所有摄像机状态
     */
    public List<AibiCameraStatusVO> listStatus() {
        List<AibiCameraStatusVO> result = new ArrayList<>(statusMap.size());
        statusMap.values().forEach(status -> {
            AibiCameraStatusVO vo = new AibiCameraStatusVO();
            vo.setCameraCode(status.getCameraCode());
            vo.setRoadCode(status.getRoadCode());
            vo.setDeviceId(status.getDeviceId());
            vo.setLeftQueueLength(status.getLeftQueueLength());
            vo.setStraightQueueLength(status.getStraightQueueLength());
            vo.setRatio(status.getRatio());
            vo.setLastSampleTime(status.getLastSampleTime());
            vo.setCurrentMode(status.getCurrentMode());
            vo.setLastSwitchTime(status.getLastSwitchTime());
            vo.setLastCommandRequestId(status.getLastCommandRequestId());
            vo.setLastCommandStatus(status.getLastCommandStatus());
            vo.setLastCommandMessage(status.getLastCommandMessage());
            result.add(vo);
        });
        return result;
    }
}

